public class Gobang {
    private static final int BOARD_SIZE = 15;
    private static final int BOARD_ILLUSTRATE_SIZE = BOARD_SIZE + 1;
    private String[][] board;

    public static int getBoardSize() {
        return BOARD_SIZE;
    }

    public void initBoard(){
        board = new String[BOARD_ILLUSTRATE_SIZE][BOARD_ILLUSTRATE_SIZE];
        for (int i = 0; i<BOARD_ILLUSTRATE_SIZE; i++){
            for(int j=0; j<BOARD_ILLUSTRATE_SIZE;j++){
                if( i == 0){
                    if( j == 0){
                        board[0][0] = j + "\t";
                    }else {
                        board[0][j] = j + (Util.countDigits(j)==1?"  ":" ");
                    }
                }else {
                    if( j == 0){
                        board[i][0] = i+"\t";
                    }else {
                        board[i][j] = "+  ";
                    }
                }
            }
        }
    }
    public void printBoard(){
        for (int i = 0; i<BOARD_ILLUSTRATE_SIZE; i++){
            for(int j=0; j<BOARD_ILLUSTRATE_SIZE; j++){
                System.out.print(board[i][j]);
            }
            System.out.print("\n");
        }
    }

    public boolean placePiece(Piece piece)
    {
        if(!board[piece.getRow()][piece.getCol()].trim().equals("+")){
            return false;
        }
        board[piece.getRow()][piece.getCol()] = piece.getColor().getUnicode()+"  ";
        return true;
    }

    public PieceColor outcome(){
        for (int i = 0; i<BOARD_ILLUSTRATE_SIZE; i++) {
            for (int j = 0; j < BOARD_ILLUSTRATE_SIZE; j++) {
                PieceColor pc = PieceColor.fromUnicode(board[i][j].trim());
                if(horizontalJudge(pc,i,j)|| verticalJudge(pc, i,j) || leftDownJudge(pc,i,j) || rightDownJudge(pc,i,j)){
                    return pc;
                }
            }
        }
        return null;
    }

    private boolean horizontalJudge(PieceColor pc, int rowStart, int colStart){
        if(pc ==null){
            return false;
        }
        for(int i=0; i<5; i++){
            if(!board[rowStart+ i][colStart].trim().equals(pc.getUnicode())){
                return false;
            }
        }
        return true;
    }

    private boolean verticalJudge(PieceColor pc, int rowStart, int colStart){
        if(pc==null){
            return false;
        }
        for(int i=0; i<5; i++){
            if(!board[rowStart][colStart+i].trim().equals(pc.getUnicode())){
                return false;
            }
        }
        return true;
    }

    private boolean rightDownJudge(PieceColor pc, int rowStart, int colStart){
        if(pc==null){
            return false;
        }
        if( (BOARD_SIZE - colStart<5) || (BOARD_SIZE-rowStart<5) )
        {
            return false;
        }
        for(int i=0; i<5; i++){
            if(!board[rowStart+i][colStart+i].trim().equals(pc.getUnicode())){
                return false;
            }
        }
        return true;
    }

    private boolean leftDownJudge(PieceColor pc, int rowStart, int colStart){
        if(pc==null){
            return false;
        }
        if( (colStart<5) || (rowStart<5) )
        {
            return false;
        }
        for(int i=0; i<5; i++){
            if(!board[rowStart+i][colStart-i].trim().equals(pc.getUnicode())){
                return false;
            }
        }
        return true;
    }



}
